package cn.edu.ujn.lab3.web;

import cn.edu.ujn.lab3.dao.BaseDict;
import cn.edu.ujn.lab3.dao.Customer;
import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.service.BaseDictService;
import cn.edu.ujn.lab3.service.CustomerService;
import cn.edu.ujn.lab3.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 字典管理控制器类
 */
@Controller
@RequestMapping("/dict")
public class DictController {
	// 依赖注入

	@Autowired
	private BaseDictService baseDictService;

	/**
	 *  字典列表
	 */
	@RequestMapping(value = "/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows,
                       String dictTypeCode, String dictTypeName,String dictItemName,
			 Model model) {
		Page<BaseDict> baseDictList = baseDictService
				.findBaseDictList(page, rows, dictTypeCode,
                        dictTypeName, dictItemName);
		model.addAttribute("page", baseDictList);
		// 添加参数
		model.addAttribute("dictTypeCode", dictTypeCode);
		model.addAttribute("dictTypeName", dictTypeName);
		model.addAttribute("dictItemName", dictItemName);
		return "dict";
	}
	
	/**
	 * 创建字典
	 */
	@RequestMapping("/create.action")
	@ResponseBody
	public String dictCreate(BaseDict baseDict,HttpSession session) {

	    // 执行Service层中的创建方法，返回的是受影响的行数
        SimpleDateFormat sf=new SimpleDateFormat("yyMMddHHmmss");
        // 字典编码自动生成
        baseDict.setDict_id(sf.format(new Date()));
	    int rows = baseDictService.createBaseDicts(baseDict);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	} 

	/**
	 * 通过id获取字典信息
	 */
	@RequestMapping("/getDictById.action")
	@ResponseBody
	public BaseDict getDictById(String id) {
        BaseDict dict = baseDictService.getBaseDictById(id);
	    return dict;
	}
	/**
	 * 更新字典
	 */
	@RequestMapping("/update.action")
	@ResponseBody
	public String dictUpdate(BaseDict baseDict) {
	    int rows = baseDictService.updateBaseDict(baseDict);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}

	/**
	 * 删除字典
	 */
	@RequestMapping("/delete.action")
	@ResponseBody
	public String dictDelete(String id) {
	    int rows = baseDictService.deleteBaseDict(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}

}

